//
//  UGraphic.cpp
//  UltraStarDeluxeIOS
//
//  Created by 李 杰军 on 12-7-15.
//  Copyright (c) 2012年 DoReiMi. All rights reserved.
//

#include <iostream>
#include "UGraphic.h"

#include "SDL_opengles.h"

SDL_GLContext glContext = NULL;
SDL_Window* window = NULL;

int RenderW = 0;
int RenderH = 0;
int ScreenW = 0;
int ScreenH = 0;
int Screens = 1;
int ScreenAct = 0;

void Initialize3D()
{
    InitializeScreen();
}

void SwapBuffers()
{
    SDL_GL_SwapWindow(window);
    glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrthof(0, RenderW, RenderH, 0, -1, 100);
    glMatrixMode(GL_MODELVIEW);
}

void InitializeScreen()
{
    int W;
    int H;
    SDL_DisplayMode fullscreenMode;
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
    
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); // Z-Buffer depth;
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    
    SDL_SetHint("SDL_IOS_ORIENTATIONS", "LandscapeRight");
    
    SDL_GetDesktopDisplayMode(0, &fullscreenMode);
    W = fullscreenMode.w;
    H = fullscreenMode.h;
    
    window = SDL_CreateWindow(NULL, 0, 0, W, H,
                                SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS);
    
    glContext = SDL_GL_CreateContext(window);
    
    if (!glContext)
    {
        fprintf(stderr, "SDL_GL_CreateContext(): %s\n", SDL_GetError());
    }
    
    SDL_GL_SetSwapInterval(0);
    
    // define virtual (Render) and real (Screen) screen size
    RenderW = 800;
    RenderH = 600;
    ScreenW = W;
    ScreenH = H;
    
    // clear screen once window is being shown
    // Note: SwapBuffers uses RenderW/H, so they must be defined before
    glClearColor(1.0f, 0, 0, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    SwapBuffers();
}
